<?php

/**
 * Iq form.
 *
 * @package    alazhar
 * @subpackage form
 * @author     Ardhy Widodo
 * @version    SVN: $Id: sfDoctrineFormTemplate.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class IqForm extends BaseIqForm {

    public function configure() {
        $this->widgetSchema['siswa_id'] = new sfWidgetFormInputHidden(array(), array('id' => 'siswa_id'));
        $this->widgetSchema['jenjang_id']->addOption('method', 'getNamaJenjang');
        $this->setWidget('no_induk', new sfWidgetFormInput(array('label' => 'No Induk Siswa'), array('id' => 'no_induk')));
        $this->setValidator('no_induk', new sfValidatorString(array('required' => true), array()));
        $this->validatorSchema->SetPostValidator(new sfValidatorCallback(array('callback' => array($this, 'checkExist'))));
        foreach (Doctrine_Core::getTable('Kecerdasan')->findAll() as $value) {
            $this->widgetSchema['kecerdasan' . $value->getId()] = new sfWidgetFormChoice(array('choices' => array('BS' => 'BS', 'B' => 'B', 'CB' => 'CB', 'C' => 'C', 'S' => 'S', 'R' => 'R', 'RS' => 'RS'), 'expanded' => true, 'label' => $value->getAspek()));
            $this->validatorSchema['kecerdasan' . $value->getId()] = new sfValidatorChoice(array('choices' => array(0 => 'BS', 1 => 'B', 2 => 'CB', 3 => 'C', 4 => 'S', 5 => 'R', 6 => 'RS'), 'required' => true));
        }
    }

    public function checkExist($validator, $values) {
        $q = Doctrine_Core::getTable('Iq')->createQuery('i')
                ->innerJoin('i.Siswa s')
                ->innerJoin('i.Jenjang j')
                ->where('s.no_induk=?', $values['no_induk'])
                ->andWhere('j.id=?', $values['jenjang_id']);
        $result = $q->fetchOne();
        if ($q->count()) {
            throw new sfValidatorError($validator, 'Data IQ untuk Siswa ' . $result->Siswa->getNamaLengkap() . ' jenjang ' . $result->Jenjang->getNamaJenjang() . ' sudah tercatat');
            //throw new sfValidatorErrorSchema($validator, array('nilai' => $error));
        }
        return $values;
    }

}
